home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / cuj1008.zip / RAMEY.ZIP / ARG.C < prev    next >
C/C++ Source or Header  |  1991-10-14  |  3KB  |  124 lines

  1. /*
  2. Copyright (c) Robert Ramey 1991. All Rights Reserved
  3. */
  4.  
  5. #include <ctype.h>
  6. #include "arg.h"
  7. /*********************************************************************
  8. arg_value - get one value from input string.  It may be decimal,
  9. octal (starting with a 0), hexidecimal (starting with 0x) or
  10. a character constant.
  11. **********************************************************************/
  12. char *
  13. arg_value(cptr, value)    /* return pointer to next character */
  14. char *cptr;                /* pointer to first character of string */
  15. int *value;                /* address of integer where result is placed */
  16. {
  17.     char c;
  18.  
  19.     *value = 0;
  20.     c = *cptr;
  21.     if(c == '\''){
  22.         /* value is character constant */
  23.         c = *++cptr;
  24.         if(c == '\\'){
  25.             switch(c = *++cptr){
  26.             case 'b': *value = '\b';break;
  27.             case 'f': *value = '\f';break;
  28.             case 'n': *value = '\n';break;
  29.             case 'r': *value = '\r';break;
  30.             case 't': *value = '\t';break;
  31.             case 'v': *value = '\v';break;
  32.             case '\\':*value = '\\';break;
  33.             default:
  34.                 error("Invalid \\character in key");
  35.             }
  36.         }
  37.         else
  38.             *value = c & 0xff;
  39.         c = *++cptr;
  40.         if(c != '\'')
  41.             error("Invalid character constant");
  42.         else
  43.             ++cptr;
  44.     }
  45.     else
  46.     if(c == '0'){
  47.         c = *++cptr;
  48.         if(c == 'x' || c == 'X'){
  49.             /* its a hexidecimal number */
  50.             while(isxdigit(c = *++cptr)){
  51.                 *value = *value * 16 + isalpha(c) ?
  52.                     tolower(c) - 'a' + 10 : c - '0';
  53.             }
  54.         }
  55.         else{
  56.             /* should be an octal digit in this case */
  57.             while(isodigit(c)){
  58.                 *value = *value * 8 + c - '0';
  59.                 c = *++cptr;
  60.             }
  61.         }
  62.     }
  63.     else{
  64.         /* it better be a decimal number */
  65.         while(isdigit(c)){
  66.             *value = *value * 10 + c - '0';
  67.             c = *++cptr;
  68.         }
  69.     }
  70.     return cptr;
  71. }
  72. /*********************************************************************
  73. arg_range - parse range of values in string.  1-4 separtes to 1 and 4.
  74. **********************************************************************/
  75. arg_range(cptr, dest)
  76. char *cptr;        /* pointer to character string containing range spec */
  77. RANGE *dest;    /* pointer to a pair of integers to recieve results */
  78. {
  79.     /* separate first number */
  80.     cptr = arg_value(cptr, &dest->start);
  81.  
  82.     /* if only one number specified */
  83.     if(*cptr == '\0'){
  84.         /* we're done. set end equal to start */
  85.         dest->end = dest->start;
  86.         return;
  87.     }
  88.  
  89.     /* if the next character is not a '-', a syntax error has been made */
  90.     if(*cptr != '-')
  91.         error("Error in collating sequence value");
  92.  
  93.     /* 1- is 1 to a HUGE number */
  94.     if(*++cptr == '\0'){
  95.         dest->end = HUGE;
  96.         return;
  97.     }
  98.  
  99.     /* otherwise get second number */
  100.     cptr = arg_value(cptr, &dest->end);
  101.     if(*cptr != '\0')
  102.         error("Error in collating sequence range");
  103.     return;
  104. }
  105. /******************************************************************
  106. arg_find - given a set of command line arguments, return index
  107. of first argument that matches the requested character string.
  108. if no command line argument matches the given string, return a -1.
  109. ******************************************************************/
  110. int
  111. arg_find(argc, argv, arg)
  112. int argc;
  113. char **argv, *arg;
  114. {
  115.     int i;
  116.  
  117.     for(i = 0;i < argc;++i){
  118.         if(!strcmp(*argv, arg))
  119.             return i;
  120.         ++argv;
  121.     }
  122.     return -1;
  123. }
  124.